const Router = require('@koa/router');
const { checkAuth, checkPermission } = require('../middlewares/auth');
const pointTransactionController = require('../controllers/pointTransaction.controller');

const router = new Router({ prefix: '/api/v1/point-transactions' });

/**
 * @swagger
 * /api/v1/point-transactions:
 *   post:
 *     tags: [积分交易]
 *     summary: 创建积分交易
 *     description: 创建新的积分获取或消费交易记录
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - ruleId
 *               - userId
 *             properties:
 *               ruleId:
 *                 type: string
 *                 description: 关联的积分规则ID
 *                 example: "rule_001"
 *               userId:
 *                 type: string
 *                 description: 用户ID
 *                 example: "user_001"
 *               description:
 *                 type: string
 *                 description: 交易说明
 *                 example: "每日签到获得积分"
 *     responses:
 *       201:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "trans_001"
 *                 points:
 *                   type: integer
 *                   example: 10
 *                 message:
 *                   type: string
 *                   example: "积分交易创建成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 规则或用户不存在
 */
router.post('/', checkAuth, checkPermission('point_transaction:create'), pointTransactionController.createTransaction);

/**
 * @swagger
 * /api/v1/point-transactions:
 *   get:
 *     tags: [积分交易]
 *     summary: 获取积分交易列表
 *     description: 获取所有积分交易记录（支持多种筛选条件）
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: userId
 *         schema:
 *           type: string
 *         description: 用户ID筛选
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *           enum: [earn, spend]
 *         description: 交易类型筛选（获取/消费）
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [pending, completed, failed, cancelled]
 *         description: 交易状态筛选
 *       - in: query
 *         name: startDate
 *         schema:
 *           type: string
 *           format: date
 *         description: 开始日期
 *         example: "2024-01-01"
 *       - in: query
 *         name: endDate
 *         schema:
 *           type: string
 *           format: date
 *         description: 结束日期
 *         example: "2024-01-31"
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *           default: 10
 *         description: 每页数量
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 total:
 *                   type: integer
 *                   example: 100
 *                 pages:
 *                   type: integer
 *                   example: 10
 *                 current:
 *                   type: integer
 *                   example: 1
 *                 transactions:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: string
 *                         example: "trans_001"
 *                       userId:
 *                         type: string
 *                         example: "user_001"
 *                       ruleId:
 *                         type: string
 *                         example: "rule_001"
 *                       type:
 *                         type: string
 *                         example: "earn"
 *                       points:
 *                         type: integer
 *                         example: 10
 *                       status:
 *                         type: string
 *                         example: "completed"
 *                       createdAt:
 *                         type: string
 *                         format: date-time
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.get('/', checkAuth, checkPermission('point_transaction:read'), pointTransactionController.getTransactions);

/**
 * @swagger
 * /api/v1/point-transactions/{id}:
 *   get:
 *     tags: [积分交易]
 *     summary: 获取交易详情
 *     description: 获取指定积分交易的详细信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 交易ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "trans_001"
 *                 userId:
 *                   type: string
 *                   example: "user_001"
 *                 ruleId:
 *                   type: string
 *                   example: "rule_001"
 *                 type:
 *                   type: string
 *                   example: "earn"
 *                 points:
 *                   type: integer
 *                   example: 10
 *                 status:
 *                   type: string
 *                   example: "completed"
 *                 description:
 *                   type: string
 *                   example: "每日签到获得积分"
 *                 metadata:
 *                   type: object
 *                   description: 交易相关的元数据
 *                 rule:
 *                   type: object
 *                   properties:
 *                     name:
 *                       type: string
 *                       example: "每日签到"
 *                     type:
 *                       type: string
 *                       example: "earn"
 *                 createdAt:
 *                   type: string
 *                   format: date-time
 *                 completedAt:
 *                   type: string
 *                   format: date-time
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 交易不存在
 */
router.get('/:id', checkAuth, checkPermission('point_transaction:read'), pointTransactionController.getTransaction);

/**
 * @swagger
 * /api/v1/point-transactions/user/{id}:
 *   get:
 *     tags: [积分交易]
 *     summary: 获取用户交易记录
 *     description: 获取指定用户的积分交易记录
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 用户ID
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *           enum: [earn, spend]
 *         description: 交易类型筛选
 *       - in: query
 *         name: startDate
 *         schema:
 *           type: string
 *           format: date
 *         description: 开始日期
 *       - in: query
 *         name: endDate
 *         schema:
 *           type: string
 *           format: date
 *         description: 结束日期
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *           default: 10
 *         description: 每页数量
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 total:
 *                   type: integer
 *                   example: 100
 *                 pages:
 *                   type: integer
 *                   example: 10
 *                 current:
 *                   type: integer
 *                   example: 1
 *                 transactions:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: string
 *                         example: "trans_001"
 *                       type:
 *                         type: string
 *                         example: "earn"
 *                       points:
 *                         type: integer
 *                         example: 10
 *                       description:
 *                         type: string
 *                         example: "每日签到获得积分"
 *                       status:
 *                         type: string
 *                         example: "completed"
 *                       createdAt:
 *                         type: string
 *                         format: date-time
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 用户不存在
 */
router.get('/user/:id', checkAuth, checkPermission('point_transaction:read'), pointTransactionController.getUserTransactions);

/**
 * @swagger
 * /api/v1/point-transactions/{id}/cancel:
 *   post:
 *     tags: [积分交易]
 *     summary: 取消积分交易
 *     description: 取消指定的积分交易（仅限未完成的交易）
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 交易ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - reason
 *             properties:
 *               reason:
 *                 type: string
 *                 description: 取消原因
 *                 example: "用户主动取消"
 *     responses:
 *       200:
 *         description: 取消成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "积分交易已取消"
 *       400:
 *         description: 无法取消（交易已完成或已取消）
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 交易不存在
 */
router.post('/:id/cancel', checkAuth, checkPermission('point_transaction:update'), pointTransactionController.cancelTransaction);

/**
 * @swagger
 * /api/v1/point-transactions/stats:
 *   get:
 *     tags: [积分交易]
 *     summary: 获取积分统计
 *     description: 获取积分交易的统计信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: userId
 *         schema:
 *           type: string
 *         description: 用户ID（可选）
 *       - in: query
 *         name: startDate
 *         schema:
 *           type: string
 *           format: date
 *         description: 开始日期
 *         example: "2024-01-01"
 *       - in: query
 *         name: endDate
 *         schema:
 *           type: string
 *           format: date
 *         description: 结束日期
 *         example: "2024-01-31"
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 totalUsers:
 *                   type: integer
 *                   example: 1000
 *                 totalPoints:
 *                   type: integer
 *                   example: 100000
 *                 averagePoints:
 *                   type: number
 *                   example: 100
 *                 dailyStats:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       date:
 *                         type: string
 *                         format: date
 *                         example: "2024-01-01"
 *                       earned:
 *                         type: integer
 *                         example: 5000
 *                       spent:
 *                         type: integer
 *                         example: 3000
 *                 topUsers:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       userId:
 *                         type: string
 *                         example: "user_001"
 *                       points:
 *                         type: integer
 *                         example: 1000
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.get('/stats', checkAuth, checkPermission('point_transaction:read'), pointTransactionController.getPointsStatistics);

module.exports = router; 